<?php
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 网城创想多用户商城 项目的一部分
//
// Copyright (c) 2007 - 2008 www.shopnc.net
//
// 要查看完整的版权信息和许可信息，请查看源代码中附带的 COPYRIGHT 文件，
// 或者访问 http://www.shopnc.net/ 获得详细信息。
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : member.php   FILE_PATH : D:\webroot\mulitishop\home\member.php
 * ....会员表现层页面
 *
 * @copyright Copyright (c) 2007 - 2007 www.shopnc.net 
 * @author 网城创想多用户商城开发团队 php_netproject@yahoo.com.cn
 * @package 
 * @subpackage 
 * @version Thu Aug 09 15:33:46 CST 2007
 */

require ("../global.inc.php");

class ShowMember extends CommonFrameWork{
	/**
	 * 会员对象
	 *
	 * @var obj
	 */
	var $objmember;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $objvalidate;

	function main(){
		
		/**
		 * 创建会员对象
		 */
		if (!is_object($this->objmember)){
			require_once ("member.class.php");
			$this->objmember = new MemberClass();
		}
		

		/**
		 * 设置模板路径
		 */
		$this->setsubtemplates("home");
		/**
		 * 语言包
		 */
		$this->getlang("member");

		/**
		 * 执行操作
		 */
		switch($this->_input['action']){
			case "check":
				//检测用户名是否存在
				$this->_checkUser();
				break;
			case "login":
				//判断是否登陆了,如果登陆则跳转到首页
				$this->isMember(true);
				$this->_login();
				break;
			case "regist":
				//判断是否登陆了,如果登陆则跳转到首页
				$this->isMember(true);
				$this->_regist();
				break;
			case "loginsave":
				$this->_dologin();
				break;
			case "registsave":
				$this->_doregist();
				break;
			case "logout":
				$this->_dologout();
				break;
			case "forget":
				//判断是否登陆了,如果登陆则跳转到首页
				$this->isMember(true);
				$this->_forget();
				break;
			default:
				//判断是否登陆了,如果登陆则跳转到首页
				$this->isMember(true);
				$this->_login();
		}

	}
	/**
	 * 会员登陆
	 *
	 */
	function _login(){
		$this->output('login_message',Common::myuncrpyt($this->_input['message']));
		$this->showpage("member.login");
	}

	/**
	 * 会员注册
	 *
	 */
	function _regist(){
		$this->showpage("member.regist");
	}

	/**
	 * 会员登陆处理
	 *
	 */
	function _dologin(){
		/**
		 * 会员登录，得到会员信息
		 */
		$member_array = $this->objmember->checkMemberExist(array("member_name"=>$this->_input['txtloginname'],"password"=>md5($this->_input['txtPassword'])),"3");
		//登录成功
		if (is_array($member_array)){
			/**
		     * 创建商铺对象
		     */
			if (!is_object($obj_shop)){
				require_once("shop.class.php");
				$obj_shop = new ShopClass();
			}
			$shop_id = $obj_shop->getShopID($member_array['login_name'],"login_name","1");  //得到商城ID
			if ($shop_id > 0){
				$_SESSION["s_shop"]['id'] = $shop_id;
				$this->shopid = $_SESSION["s_shop"]['id'];
			}
			
			$_SESSION["s_login"]['login'] = 1;
			$_SESSION["s_login"]['id'] = $member_array['member_id'];            //会员ID
			$_SESSION["s_login"]['name'] = $member_array['login_name'];    //登陆名称
			$_SESSION["s_login"]['type'] = $member_array['member_type'];  //会员类型
			/*更新最后登录时间*/
			$this->objmember->modifyMember($input_param,$_SESSION["s_login"]['id'],"last_login_time");
			/*===========================================================================
			                      与DISCUZ\X-SPASE接口部分
			===========================================================================*/
			/**
			 * 插件接口类
			 */
			require_once("api.class.php");
			
			setcookie("c_login_name",$member_array['login_name'],time()+60*24*365);
			//并把会员信息存放到COOKIE中,DISCUZ,X-SPACE接口
			$cookievalue = Api::authcode("" . $member_array['password'] . "\t$secques\t" . $member_array['member_id'] . "", 'ENCODE',$this->_configinfo['discuz']['authkey']);
			$this->setCookies("auth",$cookievalue);
			$this->setCookies("sid",'2880000');
			/*===========================================================================
			                                 结束
			===========================================================================*/
			
			//返回成功信息,并返回到登陆前访问的页面
			$this->redirectPath("refer",$this->_input['refer_url'],"");
		}else{
			//返回错误信息
			$this->redirectPath("error","",$this->_lang['errMLoginError']);  //用户名\密码不匹配，请核对后重新登录。<a href="member.php?action=login">登录</a>
		}
	}

	/**
	 * 保存注册信息
	 *
	 */
	function _doregist(){
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->objvalidate)){
			require_once("commonvalidate.class.php");
			$this->objvalidate = new CommonValidate();
		}
		
		/**
		 * 验证注册信息
		 */

		$this->objvalidate->setValidate(array("input"=>$this->_input['txtloginname'],"require"=>"true","validator"=>"Length","min"=>5,"max"=>20,"message"=>$this->_lang['alertEnterUserName']));   //5-20个字符(包括小写字母、数字、下划线、中文)，一个汉字为两个字符，推荐使用中文会员名。一旦注册成功会员名不能修改。
		$this->objvalidate->setValidate(array("input"=>$this->_input['txtPassword'],"require"=>"true","validator"=>"Length","min"=>6,"max"=>16,"message"=>$this->_lang['alertEnterPassword']));    //密码由6-16个字符组成，请使用英文字母加数字或符号的组合密码，不能单独使用英文字母、数字或符号作为您的密码。
		$this->objvalidate->setValidate(array("input"=>$this->_input['txtPassword'],"require"=>"true","validator"=>"Compare","operator"=>"==","to"=>$this->_input['txtrePassword'],"message"=>$this->_lang['errMRePassword_Wrong']));   //两次输入的密码不相同!
		$this->objvalidate->setValidate(array("input"=>$this->_input['txtemail'],"require"=>"true","validator"=>"Email","message"=>$this->_lang['errMEmail_Wrong']));    //请输入正确格式的email!
		$this->objvalidate->setValidate(array("input"=>$this->_input['txtemail'],"require"=>"true","validator"=>"Compare","operator"=>"==","to"=>$this->_input['txtecheckmail'],"message"=>$this->_lang['errMReEmail_Wrong']));   //两次输入的邮箱地址不相同!
		$this->objvalidate->setValidate(array("input"=>strtoupper($this->_input['code']),"require"=>"true","validator"=>"Compare","operator"=>"==","to"=>strtoupper($_SESSION['seccode']),"message"=>$this->_lang['errMValidateCode']));  //您输入的验证码不对!
		
		$error = $this->objvalidate->validate();
		
		if ($error != "" ){
			//返回错误信息
			$this->redirectPath("error","",$error);
		}else{
			//检查会员名称是否存在
			if ($this->objmember->checkMemberExist(array("member_name"=>$this->_input['txtloginname'])) == true){
				//如果存在返回错误信息
				$this->redirectPath("error","",$this->_lang['errExistloginname']);  //登录名称已经存在
			//检查邮件是否已经注册过
			}else if($this->objmember->checkMemberExist(array("email"=>$this->_input['txtemail'])) == true){
				//如果存在返回错误信息
				$this->redirectPath("error","",$this->_lang['errMEmailExist']);   //邮箱已经被注册，请更换其他邮箱。
			}else{
				//将会员信息放入数据库中
				$member_id = $this->objmember->addMember($this->_input);
				
				/*===========================================================================
			                      与DISCUZ\X-SPASE接口部分
			    ===========================================================================*/
				/**
				 * 注册到论坛和BLOG中
				 */
				require_once("discuz.class.php");
				$obj_discuz = new DiscuzClass();
				$this->_input['uid'] = $member_id;
				$obj_discuz->registMember($this->_input);
				/*===========================================================================
			                                 结束
				===========================================================================*/
				
				/**
				 * 注册后发信
				 */
				require_once("sendsitemail.class.php");
				$obj_sendmail = new SendSiteMail();
				$param_array = array(
				'username'=>$this->_input['txtloginname'],
			    'passwd'=>$this->_input['txtPassword']
			    );
				$obj_sendmail->SendMail("regist",$param_array,$this->_input['txtemail']);

				//注册成功后跳转到登陆页面
				$this->redirectPath("common","home/member.php?action=login","");
			}
		}

	}

	/**
	 * 检查会员名是否已被注册
	 *
	 */
	function _checkUser(){
		//检查会员是否存在
		$exist_user = $this->objmember->checkMemberExist(array("member_name"=>$this->_input['username']));
		if ($exist_user == true){
			//获取相似的会员名称
			$result = $this->objmember->getSameNoUseName($this->_input['username']);
			if (is_array($result)){
				$result = implode("|||",$result);
			}
		}else{
			$result = 0;
		}
		echo $result;
	}

	/**
	 * 退出登陆
	 *
	 */
	function _dologout(){
		setcookie("c_login_name","");
		$_SESSION["s_login"] = array();
		$_SESSION["s_shop"] = array();
		session_unregister("s_login");
		session_unregister("s_shop");
		$this->setCookies("auth","");
		$this->setCookies("sid","");
		//跳转到退出前一个页面
		$this->redirectPath("refer",$this->_input['refer_url'],"");
	}

	/**
	 * 忘记密码
	 *
	 */
	function _forget(){
		//忘记密码第一步
		if ($this->_input['step'] == '1'){
			//检查会员是否存在
			$exist = $this->objmember->checkMemberExist(array("member_name"=>$this->_input['txtloginname']));
			//抛出会员名称到页面
			$this->output('forget_login_name',$this->_input['txtloginname']);
			if ($exist == true){
				//存在的话显示忘记密码第二步页面
				$this->showpage("member.forget_two");
			}else{
				//会员不存在跳转到错误页面
				$this->redirectPath("error","",$this->_lang['errMNotExistMember']);        //会员并不存在!
			}
		//忘记密码第二步
		}else if($this->_input['step'] == '2'){
			//获取新的随机密码
			$exist = $this->objmember->getMemberNewPassword($this->_input);
			if ($exist == false){
				$this->redirectPath("error","",$this->_lang['errMGetNewPassword']);     //获得密码错误!
			}else{
				/**
				 * 获取新的密码后发信
				 */
				require_once("sendsitemail.class.php");
				$obj_sendmail = new SendSiteMail();
				$param_array = array(
				'username'=>$this->_input['txtloginname'],
				'newpass'=>$exist
			    );
				$obj_sendmail->SendMail("forget",$param_array,$this->_input['txtemail']);
				//显示第三步页面
				$this->showpage("member.forget_third");
			}
		}else{
			//显示找回密码第一步页面
			$this->showpage("member.forget_one");
		}
	}
}
$member = new ShowMember();
$member->main();
unset($member);
?>